package com.yinhe.music.yhmusic.radio;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.type.person.BannerInfo;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RadioListAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.model.RadioClassify;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.radio.contract.IRadioListContract;
import com.yinhe.music.yhmusic.radio.presenter.RadioListPresenter;
import com.yinhe.music.yhmusic.utils.ViewUtils;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;
import com.youth.banner.Banner;

import java.util.List;

/**
 * 电台列表
 *
 * @author 何晓华
 */
public class RadioListActivity extends BaseServiceActivity implements
        View.OnClickListener, BaseQuickAdapter.OnItemClickListener, IRadioListContract.IRadioListView {
    private RecyclerView mRecyclerview;
    private RadioListAdapter mAdapter;
    private TextView hotRadio;
    private TextView newRadio;

    private Banner mBanner;
    private TextView classify;
    private int classifyId = 0;
    private boolean mIsNetworkError = false;
    private static final String NEW = "new";
    private static final String HOT = "hot";
    private String sort = NEW;
    private int mPageNum;

    RadioListPresenter mPresenter;

    @Override
    public void initView() {
        RxBus.get().register(this);
        mRecyclerview = findViewById(R.id.recycler_view);

        setToolbar("电台");
        showQuickControl(true);

        initAdapter();

        initData();
    }

    @Override
    protected void onDestroy() {
        RxBus.get().unregister(this);
        super.onDestroy();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.radio_broadcast_activity;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new RadioListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void initData() {
        mPage = 1;
        mPageSize = 10;
        reqListData();
    }

    /**
     * 初始化recyclerview的adapter
     */
    private void initAdapter() {
        mAdapter = new RadioListAdapter();
        mAdapter.addHeaderView(getHeadView(LayoutInflater.from(this)));
        mAdapter.setOnItemClickListener(this);

        mAdapter.setPreLoadNumber(2);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());

        mAdapter.setOnLoadMoreListener(() -> {
            if (mPage <= mPageNum) {
                reqListData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, mRecyclerview);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this));
        mRecyclerview.setAdapter(mAdapter);
    }

    /**
     * 设置banner
     *
     * @param inflater LayoutInflater
     * @return view
     */
    public View getHeadView(final LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_radio_header, (ViewGroup) mRecyclerview.getParent(), false);

        mBanner = view.findViewById(R.id.banner);
        ViewUtils.setBanner(mBanner);

        classify = view.findViewById(R.id.radio_classify);
        classify.setOnClickListener(this);

        hotRadio = view.findViewById(R.id.hot_radio);
        newRadio = view.findViewById(R.id.new_radio);

        hotRadio.setOnClickListener(this);
        newRadio.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hot_radio:
                reqHotRadioList();
                break;
            case R.id.new_radio:
                reqNewRadioList();
                break;
            case R.id.radio_classify:
                startActivity(new Intent(RadioListActivity.this, RadioClassifyActivity.class));
                break;
            default:
                break;
        }
    }

    /**
     * 请求最热电台列表
     */
    private void reqHotRadioList() {
        hotRadio.setTextColor(getResources().getColor(R.color.menu_sort_select));
        newRadio.setTextColor(getResources().getColor(R.color.text_color_grey));
        sort = HOT;
        mPage = 1;
        reqListData();
    }

    /**
     * 请求最新电台列表
     */
    private void reqNewRadioList() {
        newRadio.setTextColor(getResources().getColor(R.color.menu_sort_select));
        hotRadio.setTextColor(getResources().getColor(R.color.text_color_grey));
        sort = NEW;
        mPage = 1;
        reqListData();
    }

    /**
     * 更新列表数据
     *
     * @param radioClassify 电台分类
     */
    public void updateList(RadioClassify radioClassify) {
        if (isDestroyed()) {
            return;
        }
        classifyId = radioClassify.getRadioClassifyId();
        mPage = 1;
        sort = NEW;
        classify.setText(radioClassify.getRadioClassifyName());
        reqListData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        RadioListInfo radioInfo = (RadioListInfo) adapter.getItem(position);
        if (radioInfo != null) {
            Intent intent = new Intent(RadioListActivity.this, RadioActivity.class);
            intent.putExtra("radioid", radioInfo.getRadioId());
            startActivity(intent);
        }
    }

    /**
     * 请求电台列表
     */
    private void reqListData() {
        mPresenter.getRadioList(mPageSize, mPage, 5, classifyId, sort);
    }

    /**
     * 设置banner数据
     */
    private void handleBannerInfo(RadioListInfo data) {
        List<BannerInfo> bannerList = data.getBannerList();
        if (!BlankUtil.isBlank(bannerList)) {
            mBanner.setOffscreenPageLimit(bannerList.size());
            ViewUtils.updateBanner(mBanner, bannerList, RadioListActivity.this);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Radio.UPDATE_RADIO_LIST)
    )
    public void onRxbusUpdateRadioList(RadioClassify radioClassify) {
        updateList(radioClassify);
    }

    /**
     * 网络状态监听回调
     *
     * @param isConnected 连接状态
     */
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Home.NETWORK_CONNECT)
    )
    public void onRxbusNetworkStatusDidChanged(Boolean isConnected) {
        if (isDestroyed()) {
            return;
        }
        if (isConnected && mIsNetworkError) {
            mIsNetworkError = false;
            mPage = 1;
            reqListData();
        } else {
            mIsNetworkError = true;
        }
    }

    @Override
    public void setRadioListUI(RadioListInfo data) {
        mRecyclerview.stopScroll();
        List<RadioListInfo> list = data.getRadioList();
        if (list == null) {
            return;
        }

        mPageNum = data.getPageNum();
        if (mPage == 1) {
            handleBannerInfo(data);
        }

        setLoadMoreList(list, mPageNum, mAdapter);
    }

    @Override
    public void setLoadMoreFailUI() {
        mAdapter.loadMoreFail();
    }
}
